const axios = require('axios');

async function testCompleteAuth() {
  try {
    console.log('🔍 完整认证功能测试...');
    
    // 测试1: 登录
    console.log('1. 测试登录...');
    const loginResponse = await axios.post('http://localhost:3001/logins', {
      username: 'admin',
      password: '123456'
    });
    
    console.log('✅ 登录成功');
    console.log('Token:', loginResponse.data.token ? loginResponse.data.token.substring(0, 20) + '...' : '无token');
    console.log('用户信息:', loginResponse.data.user);
    
    if (!loginResponse.data.token) {
      console.log('❌ 登录响应中没有token，测试终止');
      return;
    }
    
    const token = loginResponse.data.token;
    
    // 测试2: 课程API
    console.log('2. 测试课程API...');
    try {
      const coursesResponse = await axios.get('http://localhost:3001/api/courses', {
        headers: {
          'Authorization': `Bearer ${token}`
        }
      });
      console.log('✅ 课程API访问成功');
      console.log('课程数量:', coursesResponse.data.data?.length || 0);
    } catch (error) {
      console.log('❌ 课程API访问失败:', error.response?.status, error.response?.data?.message);
    }
    
    // 测试3: 教师API
    console.log('3. 测试教师API...');
    try {
      const teachersResponse = await axios.get('http://localhost:3001/api/teachers', {
        headers: {
          'Authorization': `Bearer ${token}`
        }
      });
      console.log('✅ 教师API访问成功');
      console.log('教师数量:', teachersResponse.data.data?.length || 0);
    } catch (error) {
      console.log('❌ 教师API访问失败:', error.response?.status, error.response?.data?.message);
    }
    
    // 测试4: 学生API
    console.log('4. 测试学生API...');
    try {
      const studentsResponse = await axios.get('http://localhost:3001/students', {
        headers: {
          'Authorization': `Bearer ${token}`
        }
      });
      console.log('✅ 学生API访问成功');
      console.log('学生数量:', studentsResponse.data.data?.length || 0);
    } catch (error) {
      console.log('❌ 学生API访问失败:', error.response?.status, error.response?.data?.message);
    }
    
    // 测试5: 排班API
    console.log('5. 测试排班API...');
    try {
      const schedulesResponse = await axios.get('http://localhost:3001/api/schedules', {
        headers: {
          'Authorization': `Bearer ${token}`
        }
      });
      console.log('✅ 排班API访问成功');
      console.log('排班数量:', schedulesResponse.data.data?.length || 0);
    } catch (error) {
      console.log('❌ 排班API访问失败:', error.response?.status, error.response?.data?.message);
    }
    
    // 测试6: 订单API
    console.log('6. 测试订单API...');
    try {
      const ordersResponse = await axios.get('http://localhost:3001/orders', {
        headers: {
          'Authorization': `Bearer ${token}`
        }
      });
      console.log('✅ 订单API访问成功');
      console.log('订单数量:', ordersResponse.data.data?.length || 0);
    } catch (error) {
      console.log('❌ 订单API访问失败:', error.response?.status, error.response?.data?.message);
    }
    
    // 测试7: 仪表板API
    console.log('7. 测试仪表板API...');
    try {
      const dashboardResponse = await axios.get('http://localhost:3001/dashboard/stats', {
        headers: {
          'Authorization': `Bearer ${token}`
        }
      });
      console.log('✅ 仪表板API访问成功');
      console.log('统计数据:', dashboardResponse.data.data);
    } catch (error) {
      console.log('❌ 仪表板API访问失败:', error.response?.status, error.response?.data?.message);
    }
    
    console.log('✅ 完整认证测试完成！');
    
  } catch (error) {
    console.error('❌ 测试失败:', error.response?.data || error.message);
    if (error.response) {
      console.error('状态码:', error.response.status);
      console.error('响应数据:', error.response.data);
    }
  }
}

// 运行测试
testCompleteAuth();
